#!/bin/bash
#
# sshd		Start up the OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: redis control script
#
# processname: redis

. /etc/init.d/functions

pron="monitor"
prog="/cihi/app/bin/monitor"
lockfile="/var/lock/subsys/monitor"

start() {
    if ps -ef |grep -v 'grep'|grep -q "$prog";then
        echo "Monitor already running ..."
        exit 0
    fi
    echo -n $"Starting $pron: "
    nohup $prog &> /dev/null &
    if [ $? -eq 0 ];then
        touch $lockfile
        success $"Stoping $pron: "
        echo 
    else
        failure $"Stoping $pron: "
        echo
    fi
}

stop() {
    echo -n $"Stoping $pron: "
    pid=$(ps -ef |grep "$prog" |grep -v grep|awk '{print $2}')
    kill $pid &> /dev/null
    if [ $? -eq 0 ];then
        rm -f $lockfile
        rm -f /tmp/monitor-*.txt
        success $"Stoping $pron: "
        echo 
    else
	rm -f $lockfile
        failure $"Stoping $pron: "
        echo
    fi
}

restart() {
    stop
    sleep 1
    start
}

status() {
    if ps aux|grep -v 'grep' |grep -q "$prog";then
        pidnum=$(ps aux|grep "$prog"|grep -v 'grep'|awk '{print $2}')
        echo "$pron (pid $pidnum) is running..."
    else
        echo "$pron is stoped."
    fi
}

case "$1" in 
    start)
        $1
        ;;
    stop)
        $1
        ;;
    restart)
        $1
        ;;
    status)
        $1
        ;;
    *)
        echo $"Uasge: $0 {start|stop|restart|status}"
        exit 2
esac
